package capstone.tagit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


public class TaggingActivity extends Activity {
	
	ArrayList<String> attributes;
	ArrayList<EditText> attributeFields;
	LinearLayout customAttributesLayout;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tagging);
		customAttributesLayout = (LinearLayout)findViewById(R.id.customAttributesLayout);
		Intent myIntent = getIntent();
		attributes = myIntent.getStringArrayListExtra("attributes");
		attributeFields = new ArrayList<EditText>();
		configureUI();
		Button submitButton = (Button)findViewById(R.id.submitButton);
		submitButton.setOnClickListener(submitListener);
		Button cancelButton = (Button)findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(cancelListener);		
	}
	
	private void configureUI(){
		for (String attribute : attributes){
			TextView prompt = new TextView(this);
			prompt.setText(attribute);
			EditText input = new EditText(this);
			customAttributesLayout.addView(prompt);
			customAttributesLayout.addView(input);
			attributeFields.add(input);
		}
	}
	private OnClickListener submitListener = new OnClickListener(){
		public void onClick(View view){
			Intent intent = new Intent();
			EditText titleInput = (EditText)findViewById(R.id.inputTitle);
			EditText descrInput = (EditText)findViewById(R.id.inputDescription);
			intent.putExtra("title",titleInput.getText().toString());
			intent.putExtra("description",descrInput.getText().toString());
			
			for (int i = 0; i < attributes.size(); i++){
				intent.putExtra(attributes.get(i),attributeFields.get(i).getText().toString());
			}
			setResult(0,intent);
			finish();
			
		}
	};
	private OnClickListener cancelListener = new OnClickListener(){
		public void onClick(View view){
			setResult(1, new Intent());
			finish();
			
		}
	};
}